<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <!-- 1.头部 -->
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="doShowDrawer('add')">
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 2.表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="stem" label="标题" width="400">
        </el-table-column>
        <el-table-column prop="creator" label="作者"> </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间" width="200">
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template v-slot="{row}">
            <div class="actions">
            <i class="el-icon-view" @click="doShowDrawer('preview',row.id)"></i>
            <i class="el-icon-edit-outline" @click="doShowDrawer('edit',row.id)"></i>
            <i class="el-icon-delete" @click="doDelete(row.id)"></i>
          </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 3.分页组件 -->
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        background
        :page-size="pageSize"
        :total="total"
        :page-count="9"
        :current-page="current"
        :page-sizes="[5,10,20,100]"
        @size-change="doSizeChange"
        @current-change="doCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-drawer
    :title=drawerTitle
    :visible.sync="showDrawer"
    direction = "rtl"
    size="50%"
    :before-close="handleClose"
    >
    <div v-if="drawerType === 'preview'">
      <h3>{{ formData.stem }}</h3>
      <div v-html="formData.content"></div>
    </div>
    <el-form v-else style="padding-left:20px" label-position="left" label-width="60px" :rules="formDataRules" :model="formData" ref="formDataRef">
      <el-form-item label="标题" prop="stem">
        <el-input placeholder="输入面经标题" v-model="formData.stem"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <!-- 富文本编辑器 -->
        <quillEditor v-model="formData.content"></quillEditor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="doFirm">确认</el-button>
        <el-button @click="doReset">取消</el-button>
      </el-form-item>
    </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { getArticleList, getArticleInfo, editArticle, addArticle, deleteArticle } from '@/api/article'

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: { quillEditor },
  data () {
    return {
      // 当前页
      current: 1,
      // 每页数量
      pageSize: 10,
      // 面经列表
      list: [],
      // 总数量
      total: 0,
      // 抽屉弹窗显示/隐藏
      showDrawer: false,
      // 抽屉类型
      drawerType: 'add',
      // 表单数据
      formData: {
        stem: '',
        content: ''
      },
      // 校验规则
      formDataRules: {
        stem: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  created () {
    // 页面一加载  获取面经列表
    this.getList()
  },
  methods: {
    // 获取列表
    async getList () {
      // 发送请求
      const data = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(data)
      // 绑定数据
      this.list = data.data.rows
      this.total = data.data.total
    },
    // 每页条数发生变化
    doSizeChange (size) {
      console.log(size)
      // 修改每页数量
      this.pageSize = size
      // 刷新列表
      this.getList()
    },
    // 页码变化
    doCurrentChange (page) {
      console.log(page)
      // 修改页码
      this.current = page
      // 刷新列表
      this.getList()
    },
    async doShowDrawer (type, id) {
    // (点击)新增 编辑 预览
      this.drawerType = type
      this.showDrawer = true
      // 数据回显
      if (type !== 'add') {
        const data = await getArticleInfo(id)
        console.log(data)
        // 绑定数据
        this.formData = data.data
      }
    },
    // 抽屉隐藏事件
    handleClose () {
      // 隐藏抽屉
      this.showDrawer = false
      this.formData = {
        stem: '',
        content: ''
      }
    },
    doFirm () {
      this.$refs.formDataRef.validate(async valid => {
        console.log(valid)
        if (valid) {
          if (this.formData.id) {
            await editArticle(this.formData)
            this.$message.success('修改成功') // 有id就修改,没有就新增
          } else {
            await addArticle(this.formData)
            this.$message.success('新增成功')
          }
          // 刷新列表 + 弹窗隐藏
          this.handleClose()
          this.getList()
        }
      })
    },
    async doDelete (id) {
      await deleteArticle(id)
      this.$message.success('删除成功')
      // 刷新列表
      this.getList()

      /* 删除优化 */
      if (this.total % this.pageSize === 1) {
        this.current--
      }
    },
    doReset () {
      this.$refs.formDataRef.resetFields()
      this.formData = {
        stem: '',
        content: ''
      }
    }
  },
  computed: {
    drawerTitle () {
      if (this.drawerType === 'add') {
        return '新增文章'
      } else if (this.drawerType === 'edit') {
        return '编辑文章'
      } else {
        return '文章预览'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
